﻿using System;
using System.Collections.Generic;
using System.Windows;
using Models;
using Views;

namespace SuperQuery
{
    /// <summary>
    /// Interaction logic for AddDatabases.xaml
    /// </summary>
    public partial class AddDatabases : Window, IAddDatabasesView
    {
        /// <summary>
        /// Initialise the Add Databases Window
        /// </summary>
        /// <param name="parentQueryTabView">A reference to the Query Tab view that launched the Add Databases process</param>
        public AddDatabases(IQueryView parentQueryTabView)
            : this()
        {
            m_Presenter = new Presenters.AddDatabasesPresenter(this, parentQueryTabView);
        }

        /// <summary>
        /// Initialise the Add Databases window
        /// </summary>
        private AddDatabases()
        {
            InitializeComponent();
        }

        #region Implementation of IAddDatabasesView

        /// <summary>
        /// Set up the wizard and load into the wizard a list of existing databases that are already loaded into the current
        /// Query Tab
        /// </summary>
        /// <param name="existingDatabases">The list of databases that is already loaded</param>
        public void SetupWizard(List<Database> existingDatabases)
        {
            // Clear out existing pages from the database wizard
            addDbWizard.Clear();

            // Load the "Select Server Plugin" page into the wizard
            addDbWizard.AddPage(new UserControls.AddDatabaseWizard.SelectServerPluginPage());

            // Load the "Server Details" page into the wizard
            addDbWizard.AddPage(new UserControls.AddDatabaseWizard.ServerDetailsPage());

            // Load the "Select Databases page into the wizard
            addDbWizard.AddPage(new UserControls.AddDatabaseWizard.SelectDatabasesPage(existingDatabases));

            // Start the wizard
            addDbWizard.StartWizard(null, 1);
        }

        #endregion

        /// <summary>
        /// Event raised when the wizard 
        /// </summary>
        /// <param name="sender">Who rasied the event</param>
        /// <param name="outputData">The final out put data from the wizard. This should contain a list of databases</param>
        private void addDbWizard_WizardFinished(object sender, object outputData)
        {
            // Load the list of databases into the query tab that launched the wizard
            if ((outputData != null) && (outputData is List<Database>))
            {
                m_Presenter.UpdateQueryTabDatabaseList((List<Database>)outputData);
            }

            // Close the wizard
            Close();
        }

        /// <summary>
        /// Event raised when the wizard is cancelled
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="args">Details about the event</param>
        private void addDbWizard_WizardCancelled(object sender, EventArgs args)
        {
            // If the user has cancelled then we do not need to so anything special so just close
            Close();
        }

        /// <summary>
        /// Presenter for this window
        /// </summary>
        private readonly Presenters.AddDatabasesPresenter m_Presenter;
    }
}
